import axios from './http';
const reqUrl = '';
sessionStorage.signalrHttp = reqUrl;
axios.defaults.withCredentials = true; // 让ajax携带cookie
const api = {

    // 数据推送IP返回
    getSignalrHttp () {
        return reqUrl;
    },

    get: function (url, data) {
        return axios({
            method: 'get',
            url: reqUrl + url,
            params: data,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json;charset=UTF-8',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },

    post: function (url, data = {}) {
        return axios({
            method: 'post',
            url: reqUrl + url,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },

    // 文件数据流导出
    postBlob: function (url, data) {
        return axios({
            method: 'post',
            url: reqUrl + url,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            },

            // 文件下载返回数据流
            responseType: 'blob'
        });
    },

    getExcelFile: function (url, data) {
        return axios({
            method: 'get',
            url: reqUrl + url,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            },
            'responseType': 'blob'
        });
    },

    getFile: function (url, data) {
        return axios({
            method: 'post',
            url: reqUrl + url,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            },
            'responseType': 'blob'
        });
    },

    postFile: function (url, data) {
        return axios({
            method: 'post',
            url: reqUrl + url,
            data: data,
            cache: false,
            processData: false,
            contentType: false,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },

    postUrl: function (url, set) {
        return axios({
            method: 'post',
            url: reqUrl + url + '?' + set.type + '=' + set.data.replace(/\+/g, '%2B'),
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },
    postParam: function (url, data) {
        return axios({
            method: 'post',
            url: reqUrl + url,
            params: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },
    delete: function (url, data) {
        return axios({
            method: 'delete',
            url: reqUrl + url,
            params: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },
    put: (url, data) => {
        return axios({
            method: 'put',
            url: reqUrl + url,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'IamAccessToken': window.sessionStorage.getItem('iamAccessToken'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },
    getWord: function (url, data) {
        return axios({
            method: 'get',
            url: reqUrl + url,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },
    getForm: function (url, data) {
        return axios({
            method: 'post',
            url: reqUrl + url,
            params: data,
            data: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'multipart/form-data',
                'Access-Control-Allow-Origin': reqUrl
            }
        });
    },

    download (url, data) {
        return axios({
            method: 'get',
            url: reqUrl + url,
            params: data,
            headers: {
                'Authorization': window.sessionStorage.getItem('accessToken'),
                'isSingleSignOn': window.sessionStorage.getItem('isSingleSignOn'),
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': reqUrl
            },
            'responseType': 'blob'
        });
    }
};
export default api;
